package cn.helper;

import android.os.RemoteException;
import android.util.Log;

import com.android.installreferrer.api.InstallReferrerClient;
import com.android.installreferrer.api.InstallReferrerStateListener;
import com.android.installreferrer.api.ReferrerDetails;
import com.unity3d.player.UnityPlayer;

import org.json.JSONObject;

public class GoogleInstallReferrer {

    private final static String TAG = "BToolkit";
    private static InstallReferrerClient referrerClient;

    /**
     * 线连接Google Play，后读取归因信息
     */
    public static void startConnect(String callbackGo, String callbackFun) {
        JSONObject data = new JSONObject();
        if(referrerClient != null){
            try{
                data.put("success", true);
                data.put("content", "connect_success");
            }catch (Exception e){}
            UnityPlayer.UnitySendMessage(callbackGo, callbackFun, data.toString());
            return;
        }
        referrerClient = InstallReferrerClient.newBuilder(UnityPlayer.currentActivity).build();
        referrerClient.startConnection(new InstallReferrerStateListener() {
            @Override
            public void onInstallReferrerSetupFinished(int responseCode) {
                switch (responseCode) {
                    case InstallReferrerClient.InstallReferrerResponse.OK:
                        Log.i(TAG, "[InstallReferrer] 连接 Google Play 成功");
                        try{
                            data.put("success", true);
                            data.put("content", "connect_success");
                        }catch (Exception e){}
                        UnityPlayer.UnitySendMessage(callbackGo, callbackFun, data.toString());
                        break;
                    case InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED:
                        Log.i(TAG, "[InstallReferrer] API在当前Play Store应用程序上不可用");
                        try{
                            data.put("success", false);
                            data.put("content", "InstallReferrer 在当前应用程序上不可用");
                        }catch (Exception e){}
                        UnityPlayer.UnitySendMessage(callbackGo, callbackFun, data.toString());
                        referrerClient = null;
                        break;
                    case InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE:
                        Log.i(TAG, "[InstallReferrer] 无法建立连接");
                        try{
                            data.put("success", false);
                            data.put("content", "InstallReferrer 无法建立连接 Google Play");
                        }catch (Exception e){}
                        UnityPlayer.UnitySendMessage(callbackGo, callbackFun, data.toString());
                        referrerClient = null;
                        break;
                }
            }

            @Override
            public void onInstallReferrerServiceDisconnected() {
                referrerClient = null;
                String tip = "连接已断开，下次请求 Install Referrer 前需要再次执行 startConnect()";
                Log.i(TAG, "[InstallReferrer] " + tip);
                try{
                    data.put("success", false);
                    data.put("content", tip);
                }catch (Exception e){}
                UnityPlayer.UnitySendMessage(callbackGo, callbackFun, data.toString());
            }
        });
    }

    /**
     * 取归因信息
     */
    public static String getInstallReferrer() {
        JSONObject data = new JSONObject();
        if (referrerClient == null) {
            try{
                data.put("success", false);
                data.put("content", "请先执行 startConnect() 并确保返回 connect_success");
            }catch (Exception e){}
            return data.toString();
        }
        try {
            ReferrerDetails response = referrerClient.getInstallReferrer();
            try{
                data.put("success", true);
                data.put("content", response.getInstallReferrer());
            }catch (Exception e){}
        } catch (RemoteException e) {
            try{
                data.put("success", false);
                data.put("content", e.getMessage());
            }catch (Exception ex){}
        }
        return data.toString();
    }
}
